<?php require_once('../Connections/agriculture.php'); ?>
<?php
mysql_select_db($database_agriculture, $agriculture);
$query_addVideoRecordset1 = "SELECT MAX(video_id+1) FROM video";
$addVideoRecordset1 = mysql_query($query_addVideoRecordset1, $agriculture) or die(mysql_error());
$row_addVideoRecordset1 = mysql_fetch_assoc($addVideoRecordset1);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>视频管理后台-添加视频</title>
</head>
<body>
<p><a href="admin.php">返回</a></p>

<form action="" method="post" enctype="multipart/form-data" name="form1">
<p><label>标题 <input name="textfield1" type="text" id="textfield1">
</label>
</p>
<p><label>类型 <input name="textfield2" type="text" id="textfield2">
</label>
</p>
<p>发布者</p>
<p><label>发布时间 <input name="textfield3" type="text" id="textfield3">
</label>
</p>
<p><label>作物名称 <input name="textfield4" type="text" id="textfield4">
</label>
</p>
<p><label>生长时期 <input name="textfield5" type="text" id="textfield5">
</label>
</p>
<p><label>说明信息 <textarea name="textarea6" id="textarea6"></textarea>
</label></p>
<p>视频 <label> <input type="file" name="file"></label> (仅支持.flv格式视频文件)</p>
<p><label> <input type="submit" name="Submit" value="提交"
	onclick="check(form1)"></label> <label> <input type="reset"
	name="Submit2" value="重置"></label></p>
</form>
</body>
</html>

<?php
if (is_uploaded_file ( $_FILES ['file'] ['tmp_name'] )) {
	$file = $_FILES ["file"];
	$tmp_name = $file ["tmp_name"];
	if(eregi("flv$", $file['name'])){
	$upFileName = "flash/" . $row_addVideoRecordset1['MAX(video_id+1)']. ".flv";
	$result = move_uploaded_file ( $tmp_name, $upFileName );
	}
} else {
	$upFileName = "";
}

if($_POST["textfield1"]){
$title=$_POST["textfield1"];
$class=$_POST["textfield2"];
$time=$_POST["textfield3"];
$name=$_POST["textfield4"];
$period=$_POST["textfield5"];
$information=$_POST["textarea6"];

$insertSql="INSERT INTO video (`video_id`, `video_title`, `video_class`, `video_publisher`, `video_time`, `video_name`, `video_period`, `video_information`, `video_other`) VALUES (NULL, '".$title."', '".$class."', '".$_SESSION['user']."', '".$time."', '".$name."', '".$period."', '".$information."', '".$upFileName."');";

$insertResult=mysql_query($insertSql,$agriculture) or die(mysql_error());
if($insertResult&&$result){
	echo '<script language="javascript">alert("操作成功!");window.location.href="admin.php";</script>';
}
//mysql_free_result($updateResult);;
}
?>

<script language="javascript">
function isEmpty(str){
 for(var i=0;i<str.length;i++){
  if(str.charAt(i)!==" "){
    return false;
  }
 }
 return true;
}
function check(form){
 var fileName = form.file.value;
 if(isEmpty(fileName)){
    alert("请选择文件");
    form.file.focus();
    return false;
 }
 //lastIndexOf如果没有搜索到则返回为-1
    if(fileName.lastIndexOf(".")!=-1)
 {
    var fileType = (fileName.substring(fileName.lastIndexOf(".")+1,fileName.length)).toLowerCase();
       var suppotFile = new Array();
    suppotFile[0] = "flv";
    for(var i =0;i<suppotFile.length;i++){
       if(suppotFile[i]==fileType){
     return true;
    }else{
   continue;
    }
  }
  alert("不支持的文件类型"+fileType);
  return false;
 }else
 {
  alert("文件只支持flv格式");
 }
}
</script>
